package com.bookstore.services.Impl;

import com.bookstore.mappers.BookMapper;
import com.bookstore.pojo.Book;
import com.bookstore.services.BookService;
import com.bookstore.utils.SqlSessionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 图书业务逻辑实现
 */
public class BookServiceImpl implements BookService {

    SqlSession sqlSession=SqlSessionUtil.getSqlSession();
    BookMapper mapper = sqlSession.getMapper(BookMapper.class);
    @Override
    public boolean add(Book book) {
        SqlSession sqlSession=SqlSessionUtil.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        try {
            return mapper.insert(book)>0;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            SqlSessionUtil.close(sqlSession);
        }
        return false;
    }

    @Override
    public boolean update(Book book) {
        SqlSession sqlSession=SqlSessionUtil.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        System.out.println("update:"+book);
        try {
            return mapper.updateBook(book)>0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean delete(Book book) {
        SqlSession sqlSession=SqlSessionUtil.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        try {
            return  mapper.deleteByPrimaryKey(book.getId())>0;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            SqlSessionUtil.close(sqlSession);
        }
        return false;
    }

    @Override
    public Book getOne(Book book) {
        SqlSession sqlSession=SqlSessionUtil.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        try {
            return mapper.selectByPrimaryKey(book.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            SqlSessionUtil.close(sqlSession);
        }
        return null;
    }


//    @Override
//    public Page<Book> getPage(String pageNo, String pageSize) {
////        将用户传入数据封装
//        Page<Book> page = new Page<>();
//        int pn =1;
//        int ps= page.getPageSize();
//        try {
//            pn = Integer.parseInt(pageNo);
//            pn=pn>0?pn:1;
//        } catch (Exception e) {
//            e.getMessage();
//        }
//        try {
//            ps = Integer.parseInt(pageSize);
//        } catch (Exception e) {
//            e.getLocalizedMessage();
//        }
//        return page;
//    }

    @Override
    public List<Book> getPageByPrice(String pageNo, String pageSize) {
        SqlSession sqlSession=SqlSessionUtil.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        List<Book> list = null;
        try {
            int curNum=1;
            int size=Integer.MAX_VALUE;
            try {
                pageNo=pageNo==null?"1":pageNo;
                curNum=Integer.parseInt(pageNo);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            try {
                pageSize=pageSize==null?"10":pageSize;
                size=Integer.parseInt(pageSize);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            PageHelper.startPage(curNum, size);
            list = mapper.getPageList();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            SqlSessionUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public List<Book> getPage() {
        SqlSession sqlSession=SqlSessionUtil.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        try {
            return mapper.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            SqlSessionUtil.close(sqlSession);
        }
        return null;
    }


}
